package com.ruoyi.wechat.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wechat.core.WxCore;
import com.ruoyi.wechat.domain.CampUser;
import com.ruoyi.wechat.domain.WxLoginUser;
import com.ruoyi.wechat.service.ICampUserService;
import com.ruoyi.wechat.service.IWxLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WxLoginServiceImpl implements IWxLoginService {
    @Value("${wechat.usertype}")
    private String userType;
    @Value("${wechat.wxUserRole}")
    private String wxUserRole;
    @Value("${wechat.wxUser}")
    private String wxUser;
    @Autowired
    private WxCore wxCore;
    @Resource
    private ICampUserService campUserService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysPermissionService permissionService;
    @Resource
    private ISysUserService sysUserService;
    /**
     * 微信用户登录
     * @param code
     * @param username
     * @param password
     * @return
     */
    @Override
    public WxLoginUser login(String code, String username, String password) {
        String openId = wxCore.getOpenId(code);
        if (StringUtils.isBlank(openId)){
            return  new WxLoginUser(username,"",-1,"获取openId失败");
        }
        //
        CampUser campUser = campUserService.selectCampUserByOpenId(openId);
        if (campUser==null){
            regiester(openId);
        }
        campUser = campUserService.selectCampUserByOpenId(openId);
        if (campUser!=null){
            SysUser sysUser = sysUserService.selectUserById(Long.valueOf(wxUser));
            sysUser.setUserName(campUser.getUsername());
            sysUser.setPhonenumber(campUser.getPhone());
            sysUser.setNickName(campUser.getUserId());
            sysUser.setRoleId(Long.valueOf(wxUserRole));

            //
            WxLoginUser loginUser = generate(sysUser);
            loginUser.setStatus(1);
            loginUser.setMsg("登录成功");
            loginUser.setPhone(campUser.getPhone());
            loginUser.setImageUrl(campUser.getImageUrl());
            loginUser.setUserId(campUser.getUserId());
            loginUser.setOpenId(campUser.getOpenId());
            return loginUser;
        }
        return  new WxLoginUser(username,"",-1,"登录失败");
    }

    private WxLoginUser generate(SysUser sysUser) {
        LoginUser loginUser = new LoginUser(sysUser.getUserId(),sysUser.getDeptId(),
                sysUser,permissionService.getMenuPermission(sysUser));

        String token = tokenService.createToken(loginUser);

        return new WxLoginUser(sysUser.getUserName(),"",token);
    }

    private int regiester(String openId) {
        CampUser campUser = new CampUser();
        campUser.setOpenId(openId);
        campUser.setUserId(UUID.randomUUID().toString());
        return campUserService.insertCampUser(campUser);
    }
}
